/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.uk.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.beanvalidator.BeanValidators;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.utils.excel.ImportExcel;
import com.thinkgem.jeesite.modules.uk.entity.AttendanceUk;
import com.thinkgem.jeesite.modules.uk.service.AttendanceUkService;

/**
 * English attendanceController
 * 
 * @author hui.li
 * @version 2017-03-08
 */
@Controller
@RequestMapping(value = "${adminPath}/uk/attendanceUk")
public class AttendanceUkController extends BaseController {

	@Autowired
	private AttendanceUkService attendanceUkService;

	@ModelAttribute
	public AttendanceUk get(@RequestParam(required = false) String id) {
		AttendanceUk entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = attendanceUkService.get(id);
		}
		if (entity == null) {
			entity = new AttendanceUk();
		}
		return entity;
	}

	@RequiresPermissions("uk:attendanceUk:view")
	@RequestMapping(value = { "list", "" })
	public String list(@RequestParam(required = true) String warehouse,
			AttendanceUk attendanceUk, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		// 设置仓库
		if (null != warehouse && warehouse.length() > 0) {
			attendanceUk.setWarehouse(warehouse);
		}
		Page<AttendanceUk> page = attendanceUkService.findPage(
				new Page<AttendanceUk>(request, response), attendanceUk);
		model.addAttribute("page", page);
		model.addAttribute("warehouse", warehouse);
		return "modules/uk/attendanceUkList";
	}

	@RequiresPermissions("uk:attendanceUk:view")
	@RequestMapping(value = "form")
	public String form(AttendanceUk attendanceUk, Model model) {
		model.addAttribute("attendanceUk", attendanceUk);
		return "modules/uk/attendanceUkForm";
	}

	@RequiresPermissions("uk:attendanceUk:edit")
	@RequestMapping(value = "save")
	public String save(AttendanceUk attendanceUk, Model model,
			RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, attendanceUk)) {
			return form(attendanceUk, model);
		}
		attendanceUkService.save(attendanceUk);
		addMessage(redirectAttributes, "保存English attendance成功");
		return "redirect:" + Global.getAdminPath() + "/uk/attendanceUk/?repage";
	}

	@RequiresPermissions("uk:attendanceUk:edit")
	@RequestMapping(value = "delete")
	public String delete(AttendanceUk attendanceUk,
			RedirectAttributes redirectAttributes) {
		attendanceUkService.delete(attendanceUk);
		addMessage(redirectAttributes, "删除English attendance成功");
		return "redirect:" + Global.getAdminPath() + "/uk/attendanceUk/?repage";
	}

	/**
	 * 导入英国考勤数据
	 * 
	 * @param file
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("uk:attendanceUk:edit")
	@RequestMapping(value = "import", method = RequestMethod.POST)
	public String importFile(@RequestParam(required = true) String warehouse,
			MultipartFile file, RedirectAttributes redirectAttributes) {
		if (Global.isDemoMode()) {
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/uk/attendanceUk/list?repage";
		}
		try {
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 0, 0);
			List<AttendanceUk> list = ei.getDataList(AttendanceUk.class);
			for (AttendanceUk at : list) {
				try {
					BeanValidators.validateWithException(validator, at);
					at.setWarehouse(warehouse);
					attendanceUkService.save(at);
				} catch (ConstraintViolationException ex) {
					failureMsg.append(" 导入失败：");

				} catch (Exception ex) {
					failureMsg.append("导入失败：" + ex.getMessage());
				}
			}
			addMessage(redirectAttributes, "已成功导入 ");
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入英国考勤失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + adminPath + "/uk/attendanceUk/list?warehouse="+ warehouse;
	}

	/**
	 * 下载导入考勤数据模板
	 * 
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("uk:attendanceUk:edit")
	@RequestMapping(value = "import/template")
	public String importFileTemplate(HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		try {
			String fileName = "UK实施监控记录.xlsx";
			List<AttendanceUk> list = Lists.newArrayList();
			list.add(attendanceUkService.get("22"));
			new ExportExcel("UK实施监控记录", AttendanceUk.class, 2)
					.setDataList(list).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + adminPath + "/uk/attendanceUk/list?repage";
	}

	/**
	 * 导出UK实施监控记录数据
	 * 
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @param attendanceUk
	 * @return
	 */
	@RequiresPermissions("uk:attendanceUk:view")
	@RequestMapping(value = "export", method = RequestMethod.POST)
	public String exportFile(@RequestParam(required = true) String warehouse,
			HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes, AttendanceUk attendanceUk) {
		try {
			// 设置仓库
			if (null != warehouse && warehouse.length() > 0) {
				attendanceUk.setWarehouse(warehouse);
			}
			String fileName = "UK实施监控记录" + DateUtils.getDate("yyyyMMddHHmmss")
					+ ".xlsx";
			Page<AttendanceUk> page = attendanceUkService
					.findPage(new Page<AttendanceUk>(request, response, -1),
							attendanceUk);
			new ExportExcel("UK实施监控记录", AttendanceUk.class)
					.setDataList(page.getList()).write(response, fileName)
					.dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出考勤失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + Global.getAdminPath() + "/uk/attendanceUk/?repage";
	}
}